﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DLL;
using System.Text.RegularExpressions;

namespace BLL
{
    public static class yx_BLL
    {
        //创建数据库对象
        private static dbExamDataContext db = new dbExamDataContext();
        /// <summary>
        /// 添加院系
        /// </summary>
        /// <param name="yxName">院系名称</param>
        /// <param name="zf">是否作废</param>
        public static bool Add(string yxName, bool zf)
        {
            //记录更新状态
            bool bl = false;
            //分割字符串
            string[] strs = Regex.Split(yxName, "\\r\\n", RegexOptions.None);
            //建立对象
            YX yx;
            //逐个添加
            foreach (string s in strs)
            {
                try
                {
                    //排除重复项
                    if (db.YX.Single(p => p.yxName == s).yxNum > -1)
                        continue;
                }
                catch { }
                yx = new YX();
                yx.yxName = s;
                yx.yxZF = zf;
                //指定要添加的对象
                db.YX.InsertOnSubmit(yx);
                //执行添加
                db.SubmitChanges();
                bl = true;
            }
            return bl;
        }

        /// <summary>
        /// 更改数据项
        /// </summary>
        /// <param name="yxId">编号</param>
        /// <param name="yxName">名称</param>
        /// <param name="zf">是否作废</param>
        public static bool Update(string yxId, string yxName, bool zf)
        {
            //记录更新状态
            bool bl = false;
            try
            {
                //获取要更改的数据项
                YX yx = db.YX.Single(p => p.yxNum == decimal.Parse(yxId));
                yx.yxName = yxName;
                yx.yxZF = zf;
                //提交更改
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }

        /// <summary>
        /// 删除数据项
        /// </summary>
        /// <param name="yxId">要删除的数据标识</param>
        public static bool Delete(string yxId)
        {
            db = new dbExamDataContext();
            //记录更新状态
            bool bl = false;
            //找到要删除的项
            YX yx=db.YX.Single(p=>p.yxNum==decimal.Parse(yxId));
            //执行删除
            db.YX.DeleteOnSubmit(yx);
            //提交更改
            db.SubmitChanges();
            return bl;
        }

        /// <summary>
        /// 获取数据源
        /// </summary>
        public static object GetSource()
        {
            return from p in db.YX select new {p.yxNum,p.yxName,yxZF=(p.yxZF==true?"是":"否") };
        }        
    }
}
